﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VisualAgile.IBLL;
using VisualAgile.Model;
using System.Collections;

namespace VisualAgile.Web.Tests.Fakes
{
    public class FakeFeelingsManager : IFeelingsManager   
    {
        private List<Feelings> feelingList;

        public FakeFeelingsManager(List<Feelings> feelings)
        {
            feelingList = feelings;
        }

        /// <summary>
        /// Create a feeling (back to contain the default value)
        /// </summary>
        /// <returns></returns>
        public Feelings NewFeeling()
        {
            var feeling = new Feelings();
            
            feeling.CreateTime = DateTime.Now;            

            return feeling;
        }

        /// <summary>
        /// Save feeling
        /// </summary>
        /// <param name="feeling">Feelings entity</param>
        /// <returns></returns>
        public Feelings Save(Feelings feeling)
        {
            Validate(feeling);
            if (Get(feeling.UserMoodID) != null)
                throw new Exception("Existing feeling");
            feeling.UserMoodID = Guid.NewGuid();
            feelingList.Add(feeling);

            return Get(feeling.UserMoodID);
        }

        /// <summary>
        /// Access to feelings
        /// </summary>
        /// <param name="feelingId">Feelings ID</param>
        /// <returns></returns>
        public Feelings Get(Guid feelingId)
        {
            return feelingList.SingleOrDefault(u => u.UserMoodID == feelingId);
        }        

        /// <summary>
        /// Access to feelings by user
        /// </summary>
        /// <param name="feelingId">User ID</param>
        /// <returns></returns>
        public IList GetByUser(string userId)
        {
            return feelingList.FindAll(f => f.UserID == userId);
        }

        /// <summary>
        /// Delete Feelings
        /// </summary>
        /// <param name="feelingId">Feelings ID</param>
        /// <returns></returns>
        public void Delete(Guid feelingId)
        {
            var feeling = Get(feelingId);
            feelingList.Remove(feeling);
        }

        /// <summary>
        /// Update Feelings
        /// </summary>
        /// <param name="feeling">Feelings Entity</param>
        /// <returns></returns>
        public Feelings Update(Feelings feeling)
        {
            Feelings oldFeelings = Get(feeling.UserMoodID);
            if (oldFeelings != null)
                feelingList.Remove(oldFeelings);
            else
                feeling.UserMoodID = Guid.NewGuid();

            feelingList.Add(feeling);
            return Get(feeling.UserMoodID);
        }

        /// <summary>
        /// Access to feelings
        /// </summary>
        /// <returns></returns>
        public IList GetAll()
        {
            return feelingList;
        }

        private void Validate(Feelings feeling)
        {
            //TODO throw exception on error.
        }        
    }
}
